package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.BaseItem;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.DictItemService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：字典条目Controller
 * @date ：2020/8/17 14:18
 */
@Controller
@RequestMapping("/dictItem")
public class DictItemController {

    @Autowired
    private DictItemService dictItemService;

    @Autowired
    private UserUtils userUtils;



    /**
     * @description 获取字典条目条目列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResResult getDictList(BaseItem item, Integer page, Integer limit, String searchContent, Integer dictId) {
        if (!StringUtils.isEmpty(searchContent)) {
            item.setItemName(searchContent);
        }
        if (dictId != null) {
            item.setDictId(dictId);
        }
        ResResult resResult = dictItemService.listByPage(item, page, limit);
        resResult.setOptName("获取字典条目列表");
        return resResult;
    }

    /**
     * @description 保存字典条目信息
     * @author XieXing
     * @date 2020/8/17 14:22
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveDict(BaseItem item) {
        User user = userUtils.getCurrentUser();//获取当前
        item.setItemAddby(user.getUserName());
        item.setItemAddTime(new Date());
        item.setItemStatus(1);
        item.setItemAddbyId(user.getId());
        ResResult resResult = dictItemService.insert(item);
        resResult.setOptName("保存字典条目信息");
        return resResult;
    }


    /**
     * @description 更新字典条目信息
     * @author XieXing
     * @date 2020/8/17 22:03
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateDict(BaseItem item) {
        User user = userUtils.getCurrentUser();
        item.setItemUpdateby(user.getUserName());
        item.setItemUpdateTime(new Date());
        item.setItemUpdatebyId(user.getId());
        ResResult resResult = dictItemService.updateByPrimaryKeySelective(item);
        resResult.setOptName("更新字典条目信息");
        return resResult;
    }

    /**
     * @description 删除字典条目 逻辑删除
     * @author XieXing
     * @date 2020/8/17 23:02
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delDict(@PathVariable(value = "id") Integer id) {
        ResResult resResult = dictItemService.deleteByPrimaryKey(id);
        resResult.setOptName("删除字典条目信息");
        return resResult;
    }


    /**
     * @description 更新字典条目状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDictStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        ResResult resResult = dictItemService.updateDataStatus(id, status);
        resResult.setOptName("更新字典条目条目状态");
        return resResult;
    }

}
